﻿//***********************************************************************
//* Module:  TbBstCarDAL
// * 作者:  曹荣芝
//* Modified: 2013-05-09
// * Purpose: Definition of the Class TbBstCarDAL
// ***********************************************************************/
using System;
using System.Data;
using XWG.Framework.AuthEntity;
using XWG.JWBST.JWBSTEntity;
using XWG.Framework.AuthSPConst;
using XWG.Framework.AuthUtility;
using XWG.JWBST.JWBSTSPConst;
using XWG;
using XWG.Framework.MSException;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using NHibernate;
using System.Data.OracleClient;
namespace XWG.JWBST.JWBSTDAL
{
    /// <summary>
    /// 
    /// </summary>
    public class TbBstCarDAL
    {
        #region 删除操作
        /// <summary>
        /// 删除。
        /// </summary>
        /// <param name="carpkid">要删除的的主键值。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>是否成功 1 表示成功 负值代表失败</returns>
        public static ResultModel Delete(object carpkid, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();//定义错误信息类
            try
            {
                //载入实例
                JWBSTEntity.TbBstCar u = JWBSTDAL.TbBstCarDAL.Load(carpkid) as JWBSTEntity.TbBstCar;

                if (isLog)//如果写入日志
                {
                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "删除";
                    log.LogObject = TbBstCarConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransDelete<TbBstCar, AppLog>(u, log);
                }
                else//不写入日志
                {
                    Db.TransDelete(u);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(carpkid);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9994;
                resultModel.FiledKey = Convert.ToString(carpkid);
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的删除操作");
                return resultModel;

            }
        }
        #endregion

        #region 根据主键查询一条记录
        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="carpkid">主键</param>
        /// <returns>用户实体</returns>
        public static JWBSTEntity.TbBstCar Load(object carpkid)
        {
            return Db.GetModel<JWBSTEntity.TbBstCar>(carpkid);
        }
        #endregion

        #region 查询操作
        /// <summary>
        /// 查询
        /// </summary>
        public static IList<TbBstCar> SelectList()
        {
            try
            {
                ICriteria crt = Db.Session.CreateCriteria(typeof(TbBstCar));
                //crt.Add(NHibernate.Criterion.Expression.Eq(TbBstCarConst.Columns.EnabledPropertyName, Enabled.TRUE));
                IList<TbBstCar> list = crt.List<TbBstCar>();
                return list;
            }
            catch (Exception ex)
            {
                XWG.Framework.MSException.MSEx.DealErr(ex, "查询");
                return null;
            }
            finally
            {

            }
        }
        #endregion
        #region 插入操作
        /// <summary>
        /// 新增。
        /// </summary>
        /// <param name="info">待新增的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>新增是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Insert(JWBSTEntity.TbBstCar info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            object filedkey;
            try
            {
                if (isLog)
                {

                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "新建";
                    log.LogObject = TbBstCarConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    filedkey = Db.TransInsert<TbBstCar, AppLog>(info, log);
                }
                else
                {
                    filedkey = Db.TransInsert<TbBstCar>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = Convert.ToString(filedkey);
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {

                resultModel.ErrorCode = -9998;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的插入操作");
                return resultModel;
            }
        }
        #endregion

        #region 修改操作
        /// <summary>
        /// 修改。
        /// </summary>
        /// <param name="info">待修改的信息。</param>
        /// <param name="userid">操作员。</param>
        /// <param name="isLog">是否写入日志。</param>
        /// <returns>修改是否成功的标示。（1：成功；负值失败）</returns>
        public static ResultModel Update(JWBSTEntity.TbBstCar info, object userid, bool isLog)
        {
            ResultModel resultModel = new ResultModel();
            try
            {
                if (isLog)
                {


                    AppLog log = new AppLog();
                    log.UserId = Convert.ToString(userid);
                    log.Id = System.Guid.NewGuid().ToString();
                    log.LogContent = "修改";
                    log.LogObject = TbBstCarConst.TableName;
                    log.LogType = "操作日志";
                    log.Opratetime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                    Db.TransUpdate<TbBstCar, AppLog>(info, log);
                }
                else
                {
                    Db.TransUpdate<TbBstCar>(info);
                }
                resultModel.ErrorCode = 0;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = true;
                return resultModel;
            }
            catch (Exception ex)
            {
                resultModel.ErrorCode = -9997;
                resultModel.FiledKey = "";
                resultModel.ISSucceed = false;
                MSEx.DealErr(ex, "实体类的修改操作");
                return resultModel;
            }
        }
        #endregion

        #region 检验车辆数量
        /// <summary>
        /// 根据用户ID判断是车辆是否超过Carnum辆
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="Carnum"></param>
        /// <returns></returns>
        public static bool CheckNum(string userid, int Carnum)
        {
            string sql = "select count(*) from tb_bst_car where user_id = :userid and isaudit = '1'";   //根据用户ID查找未审核的和已经通过审核的车的数量

            OracleParameter[] parameters = {
					new OracleParameter(":userid",userid)
                                           };
            int num = Convert.ToInt32(OrclHelper.ExecuteScalar(sql, parameters));
            if (Carnum > num )
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 重复录入检测
        /// <summary>
        /// 根据用户ID和车牌号检测是否重复录入
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="carlicense"></param>
        /// <returns></returns>
        public static bool CheckRepeat(string userid, string carlicense)
        {
            string sql = "select count(*) from tb_bst_car where user_id = :userid and car_carlicense =:carlicense and ISDEL = '0'";

            OracleParameter[] parameters = {
					new OracleParameter(":userid",userid),
                    new OracleParameter(":carlicense",carlicense)
                                           };
            int num = Convert.ToInt32(OrclHelper.ExecuteScalar(sql, parameters));
            if (num == 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        #endregion

        #region 查询该用户名下车辆审核状态(未删除)
        /// <summary>
        /// 根据ID查找该ID下所有车辆审核信息
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static List<TbBstCar> CheckCarByUserID(string userID)
        {
            List<TbBstCar> carlist = new List<TbBstCar>();
            string sql = "select * from tb_bst_car where USER_ID =:userid and ISDEL = '0'";
            OracleParameter[] parameters = {
					new OracleParameter(":userid",userID)
                                           };
            DataTable mes = OrclHelper.GetDT(sql, parameters);
            if (mes != null && mes.Rows.Count > 0)
            {
                for (int i = 0; i < mes.Rows.Count; i++)
                {
                    TbBstCar car = new TbBstCar();
                    car.CarPkid = mes.Rows[i]["CAR_PKID"].ToString();
                    car.CarCarlicense = mes.Rows[i]["CAR_CARLICENSE"].ToString();
                    car.Isaudit = mes.Rows[i]["ISAUDIT"].ToString();
                    carlist.Add(car);
                }
            }
            return carlist;
        }

        /// <summary> 
        /// 根据ID和审核状态 查找该ID下所有车辆  (未删除)
        /// </summary>
        /// <param name="userID"></param>
        /// <returns></returns>
        public static List<TbBstCar> CheckCarByUserIDAndAudit(string userID,string audit)
        {
            List<TbBstCar> carlist = new List<TbBstCar>();
            string sql = "select * from tb_bst_car where USER_ID =:userID and ISAUDIT =:isaudit and ISDEL = '0'";
            OracleParameter[] parameters = {
					new OracleParameter(":userid",userID),
                    new OracleParameter(":isaudit",audit)
                                           };
            DataTable mes = OrclHelper.GetDT(sql, parameters);
            if (mes != null && mes.Rows.Count > 0)
            {
                for (int i = 0; i < mes.Rows.Count; i++)
                {
                    TbBstCar car = new TbBstCar();
                    car.CarPkid = mes.Rows[i]["CAR_PKID"].ToString();
                    car.CarCarlicense = mes.Rows[i]["CAR_CARLICENSE"].ToString();
                    car.Isaudit = mes.Rows[i]["ISAUDIT"].ToString();
                    carlist.Add(car);
                }
            }
            return carlist;
        }


        ///// <summary>
        ///// 根据用户名和车牌号获取该用户下所有通过审核的车主GUID
        ///// </summary>
        ///// <param name="userID"></param>
        ///// <param name="Carno"></param>
        ///// <returns></returns>
        //public static int IsAudit(string userID, string Carno)
        //{
        //    string sql = "select count(*) from tb_bst_car where user_id = :userid and CAR_CARLICENSE = :Carno and ISDEL = '0' and isaudit = '1'";
        //    OracleParameter[] parameters = {
        //            new OracleParameter(":userid",userID),
        //            new OracleParameter(":Carno",Carno)
        //                                   };
        //    int i = Convert.ToInt32(OrclHelper.ExecuteScalar(sql,parameters));
        //    //int i = OrclHelper.ExecuteScalar(sql, parameters);
        //    //int c = 0;
        //    return i;
        //}
        #endregion
    }
}
